/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleshipgame.logic;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author אלירן
 */
public class AIActionGenerator {
    ArrayList<Player> enemies ;
    ArrayList<Command> commands ;
    Player me;
    int commandsSize=0;

    public AIActionGenerator( Player me) {
        commandsSize = 0;
        this.me = me;
    }
    public void initEnemies (ArrayList<Player> players, int numofplayers){
        int i;
        for ( i=0 ; i<numofplayers ; i++)
            if (players.get(i) != me)
                enemies.add(players.get(i));
    }
            
    
    public void  AIActionGeneratorinit (ArrayList<Player> enemies) {
        this.enemies = enemies;
    }
   public Command generateRandomCommand (){
       Random random = new Random() ;
       int size = enemies.size();
       int playerNum =generateRandomInteger(0, size, random);
       Player player = enemies.get(playerNum);
       int x = generateRandomInteger(0, Constants.BOARD_SIZE, random);
       int y = generateRandomInteger(0, Constants.BOARD_SIZE, random);
       Coordinate coordinate = new Coordinate(x, y);
       Command command =new Command(player, coordinate);
       commands.add(command);
       return command ;
   }
   public Command generateCommandByDiagonalSearch ( Player player ) {
       Coordinate coordinate = commands.get(commandsSize).getCoordinate() ;
       int x =coordinate.getX();
       int y= coordinate.getY() ;
       if ( new Coordinate(x+1, y+1).IsValidLocationInBoard())
                  return new Command(player, new Coordinate(x+1, y + 1));
       if ( new Coordinate(x-1, y-1).IsValidLocationInBoard())
           return new Command(player, new Coordinate(x-1, y -1));
       if ( new Coordinate(x+1, y-1).IsValidLocationInBoard())
           return new Command(player, new Coordinate(x+1, y - 1));
       if ( new Coordinate(x-1, y+1).IsValidLocationInBoard())
           return new Command(player, new Coordinate(x-1, y + 1));
       return generateRandomCommand();
   }
   public Command DestroyShipSequence (Coordinate coordinate , Player player){
       int x =coordinate.getX();
       int y= coordinate.getY() ;    
       if (player.IsPlayerDead())
               return generateRandomCommand() ;
       else{
           if ( new Coordinate(x+1, y).IsValidLocationInBoard())
               return new Command(player, new Coordinate(x+1, y ));
       if ( new Coordinate(x-1, y).IsValidLocationInBoard())
           return new Command(player, new Coordinate(x-1, y ));
       if ( new Coordinate(x, y-1).IsValidLocationInBoard())
           return new Command(player, new Coordinate(x, y - 1));
       if ( new Coordinate(x, y+1).IsValidLocationInBoard() )
           return new Command(player, new Coordinate(x, y + 1));
       }
       return generateRandomCommand();
   }
   
    private static int generateRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);
    return randomNumber;
  }
  
  
}
